package com.drojian.demo3.service.local;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.drojian.demo.R;
import com.drojian.demo3.service.local.MyLocalService;

public class TestActivity1 extends AppCompatActivity implements View.OnClickListener {
    private Button startService,stopService,bindService,unbindService;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.part3_activity_test1);
        initView();
    }

    private void initView() {
        startService= findViewById(R.id.start_service);
        stopService= findViewById(R.id.stop_service);
        bindService= findViewById(R.id.bind_service);
        unbindService= findViewById(R.id.unbind_service);
        startService.setOnClickListener(this);
        stopService.setOnClickListener(this);
        bindService.setOnClickListener(this);
        unbindService.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.start_service:
                startLocalService();
                break;
            case R.id.stop_service:
                stopLocalService();
                break;
           case R.id.bind_service:
                bindLocalService();
                break;
           case R.id.unbind_service:
                unbindLocalService();
                break;
        }
    }

    private ServiceConnection conn;
    private void unbindLocalService() {
        if(conn != null){
            unbindService(conn);
            conn = null;
            Toast.makeText(this,"解绑成功",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"未进行绑定",Toast.LENGTH_SHORT).show();

        }
    }
    private void bindLocalService() {
       if(conn == null){
           Intent intent = new Intent(this, MyLocalService.class);
           conn = new ServiceConnection(){
               @Override
               public void onServiceConnected(ComponentName name, IBinder service) {
                   Log.e("xjq", "TestActivity1   onServiceConnected()");
               }

               @Override
               public void onServiceDisconnected(ComponentName name) {
                   Log.e("xjq", "TestActivity1   onServiceDisconnected()");
               }
           };
           bindService(intent,conn, Context.BIND_AUTO_CREATE);
           Toast.makeText(this,"绑定成功",Toast.LENGTH_SHORT).show();
       }else {
           Toast.makeText(this,"已经进行了绑定",Toast.LENGTH_SHORT).show();
       }
    }


    private void startLocalService() {
        Intent intent = new Intent(this, MyLocalService.class);
        startService(intent);
        Log.e("xjq", "TestActivity1   startLocalService()");
        Toast.makeText(this,"服务启动成功",Toast.LENGTH_SHORT).show();

    }

    private void stopLocalService() {
        Intent intent = new Intent(this, MyLocalService.class);
        stopService(intent);
        Log.e("xjq", "TestActivity1   stopLocalService()");
        Toast.makeText(this,"服务停止成功",Toast.LENGTH_SHORT).show();


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(conn != null){
            unbindService(conn);
            conn = null;
        }
    }
}